package myauth

import (
	"data/config"
	"public/mycache"
	"public/mylog"
	"public/mysqldb"
	"strconv"
	"time"
)

//用户配置表
var auth_config map[int][]Auth_info

func init() {
	loadAuthConfigInfo()
}

//获取用户权限配置信息
func loadAuthConfigInfo() {
	//清空配置
	auth_config = make(map[int][]Auth_info)
	//读取配置信息
	auth_config = GetAuthConfigInfo()
}

//获取权限配置信息
func GetAuthConfigInfo() (auth_config map[int][]Auth_info) {
	//读取配置信息
	cache := mycache.OnGetCache("auth_config_info")
	obj, b := cache.Value("common")
	if b {
		auth_config = obj.(map[int][]Auth_info)
	} else {
		var tmp = make(map[int][]Auth_info)
		var db mysqldb.MySqlDB
		defer db.OnDestoryDB()
		orm := db.OnGetDBOrm(config.GetDbUrl())

		var config []Authority_config_tbl
		orm.Find(&config)

		if len(config) > 0 {
			for _, v := range config {
				var auths []Auth_info
				var tm Auth_info
				tm.Module = v.Module
				tm.Auth_level = v.Auth_level
				tm.Des = v.Des
				tm.Auth_type = v.Auth_type
				tm.Topic = v.Topic
				tm.Bundle = v.Bundle
				tm.Pid = v.Pid

				if tmp[v.Role] != nil {
					auths = tmp[v.Role]
				}
				auths = append(auths, tm)
				tmp[v.Role] = auths
			}
		}
		cache.Add("common", tmp, 24*time.Hour)
		auth_config = tmp
	}
	return
}

//查找某个功能模块权限
func findAuthInfoByModule(auths []Auth_info, module string) (auth Auth_info) {
	if len(auths) > 0 {
		for _, v := range auths {
			if v.Module == module {
				auth = v
			}
		}
	}
	return
}

/*
	user:用户信息
	module:功能模块标识
	levels:功能模块标识支持的全部级别
*/
func CheckUserAuth(user UserInfo, module string, levels ...string) (r bool) {
	r = false
	//获取用户信息
	// cache := mycache.OnGetCache("user_info")
	// var tp interface{}
	// tp, b := cache.Value(username)
	// if !b { //未找到
	// 	mylog.Debug("未找到此用户")
	// 	return
	// }
	// user_tbl := tp.(UserInfo)
	auths := auth_config[user.Business_role]          //用户具有的各module的全部权限
	auth_level := findAuthInfoByModule(auths, module) //用户对于model实际的权限

	for _, e := range levels {
		if e == auth_level.Auth_level { //权限级别对应
			r = true
			if auth_level.Auth_type == TYPE_TOPIC { //判断用户所属topic
				if auth_level.Topic != "" && (auth_level.Topic != user.Topic) {
					r = false
				}
			}
			if auth_level.Auth_type == TYPE_BUNDLE { //判断用户所属bundle
				if auth_level.Bundle != "" && (auth_level.Topic != user.Topic || auth_level.Bundle != user.Bundle) {
					r = false
				}
			}
			if auth_level.Auth_type == TYPE_PID { //判断用户所属pid
				if auth_level.Pid != "" && (auth_level.Topic != user.Topic || auth_level.Bundle != user.Bundle || auth_level.Pid != user.Pid) {
					r = false
				}
			}
		}
	}
	return
}

//删除权限
func DeleteAuthConfigInfo() bool {
	//读取配置信息
	cache := mycache.OnGetCache("auth_config_info")
	// err := cache.Delete("common")
	return cache.Clear()
}

//编辑权限sql
func UpdateAuthConfigInfo(sql string, values ...interface{}) bool {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	err := orm.Exec(sql, values...).Error
	if err != nil {
		mylog.Error(err)
		return false
	}
	//删除
	DeleteAuthConfigInfo()
	loadAuthConfigInfo()
	return true
}

//编辑权限对象
func SaveAuthConfigInfo(record Authority_config_tbl) bool {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var tmp Authority_config_tbl
	orm.Where("role = ? and module = ? ", record.Role, record.Module).Find(&tmp)
	record.Id = tmp.Id
	err := orm.Save(&record).Error
	if err != nil {
		mylog.Error(err)
		return false
	}
	//删除
	DeleteAuthConfigInfo()
	loadAuthConfigInfo()
	return true
}

//获取权限配置信息
func GetAuthConfigDetailInfo() (auth_config map[string]Auth_info) {
	//读取配置信息
	cache := mycache.OnGetCache("auth_config_info")
	obj, b := cache.Value("detail")
	if b {
		auth_config = obj.(map[string]Auth_info)
	} else {
		auth_config = make(map[string]Auth_info)
		var db mysqldb.MySqlDB
		defer db.OnDestoryDB()
		orm := db.OnGetDBOrm(config.GetDbUrl())

		var config []Authority_config_tbl
		orm.Find(&config)

		if len(config) > 0 {
			for _, v := range config {
				var tm Auth_info
				tm.Module = v.Module
				tm.Auth_level = v.Auth_level
				tm.Des = v.Des
				tm.Auth_type = v.Auth_type
				tm.Topic = v.Topic
				tm.Bundle = v.Bundle
				tm.Pid = v.Pid
				auth_config[strconv.Itoa(v.Role)+"_"+v.Module] = tm
			}
		}
		cache.Add("detail", auth_config, 24*time.Hour)
	}
	return
}
